<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{ this.$store.state.city }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="item in hotCities"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <div class="button">{{ item.name }}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{ key }}</div>
        <ul class="item-list">
          <li
            class="item border-bottom"
            v-for="innerItem in item"
            :key="innerItem.id"
            @click="handleCityClick(innerItem.name)"
          >{{ innerItem.name }}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    cities: Object,
    hotCities: Array,
    value: String
  },
  methods: {
    handleCityClick (name) {
      this.$store.dispatch('changeCity', name)
      this.$router.push('/')
    }
  },
  watch: {
    value (val) {
      this.scroll.scrollToElement(this.$refs[val][0])
    }
  },
  mounted () {
    //  在页面渲染完创建 BScroll 对象
    this.scroll = new Bscroll(this.$refs.wrapper)
  }
}
</script>

<style scoped lang="less">
.list{
  position: absolute;
  top: 1.48rem;
  left: 0;
  bottom: 0;
  right: 0;
  overflow: hidden;
  .area{
    .title{
      padding: 0.15rem 0.2rem;
      background-color: #f5f5f5;
      font-size: 0.24rem;
    }
    .button-list{
      padding: 0.1rem 0.3rem 0.1rem 0.1rem;
      overflow: hidden;
      .button-wrapper{
        width: 33.3%;
        float:left;
        text-align: center;
        .button{
          margin: 0.1rem 0.2rem;
          border: 1px solid #ccc;
          border-radius: 0.06rem;
          text-align: center;
          height: 0.4rem;
          line-height: 0.4rem;
        }
      }
    }
  }
  .item-list{
    padding: 0.1rem 0.3rem 0.1rem 0.3rem;
    .item{
      line-height: 0.6rem;
    }
  }
}
</style>
